<template>
  <basic-container>
    <avue-crud :option="option" :table-loading="loading" :data="data" :page.sync="page" :permission="permissionList"
      :before-open="beforeOpen" :search.sync="search" v-model="form" ref="crud" @row-update="rowUpdate"
      @row-save="rowSave" @row-del="rowDel" @search-change="searchChange" @search-reset="searchReset"
      @selection-change="selectionChange" @current-change="currentChange" @size-change="sizeChange"
      @refresh-change="refreshChange" @on-load="onLoad" @tree-load="treeLoad">
      <!-- 自定义操作列 -->
      <template #menu="{ row, index, size }">
        <el-tooltip v-if="row.status == 1" class="item" effect="dark" content="订单发货" placement="top">
          <el-button type="primary" :size="'mini'" icon="el-icon-truck" style="width: 23px;
        height: 22px; padding: 0;" @click="handleDelivery(row, index)"></el-button>
        </el-tooltip>
      </template>
      <!-- 单元格 -->
      <!-- <template slot-scope="{ row }" slot="status">
        <p v-if="row.status == 0" class="normal">正常</p>
        <p v-else-if="row.status == 1" class="un-normal">禁用</p>
      </template> -->
    </avue-crud>
    <!-- 发货弹窗 -->
    <el-dialog :visible.sync="isShowModal" append-to-body destroy-on-close title="订单发货" width="40%">
      <el-form ref="formDataRef" :rules="formRules" :model="formData">
        <el-form-item label="物流公司：" prop="logistics_name">
          <el-input type="textarea" v-model="formData.logistics_name"></el-input>
        </el-form-item>
        <el-form-item label="物流单号：" prop="logistics_tracking_no">
          <el-input type="textarea" v-model="formData.logistics_tracking_no"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm()"> 取消 </el-button>
        <el-button type="primary" @click="submitForm()"> 确认 </el-button>
      </div>
    </el-dialog>
  </basic-container>
</template>

<script>
import { getProductOrderList, getDeliveryProductOrderData } from "./api/productOrder";
import { mapGetters } from "vuex";
import { getSystemUserList } from "../systemManage/systemUserManage/api/user";
export default {
  data() {
    return {
      formRules: {
        logistics_name: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
        ],
        logistics_tracking_no: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
        ],
      },
      formData: {},
      isShowModal: false,
      form: {},
      query: {},
      search: {}, // 收索默认值
      loading: true,
      page: {
        currentPage: 1,
        pageSize: 10,
        total: 0,
      },
      selectionList: [],
      option: {
        menuType: 'button',
        height: "auto",
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: false,

        index: true,
        indexLabel: '序号',
        searchBtnText: '提交',
        searchBtnIcon: '提交',
        emptyBtnText: '重置',
        emptyBtnIcon: '重置',
        viewBtn: false,
        addBtn: false,
        addBtnText: '添加',
        addBtnIcon: 'el-icon-plus',
        editBtn: false,
        editBtnText: '',
        selection: false,
        dialogClickModal: false,
        delBtn: false,
        delBtnText: '',
        labelWidth: 110,
        searchLabelWidth: 100,
        column: [
          {
            label: "订单号",
            prop: "order_no",
            search: true,
          },
          {
            label: "用户",
            prop: "uid",
            type: 'select',
            search: true,
            hide: true,
            dicData: [],
          },

          {
            label: "支付单号",
            prop: "pay_no",
            search: true,
          },
          {
            label: "订单类型",
            prop: "order_type",
            type: 'select',
            search: true,
            dicData: [{
              label: '普通订单',
              value: 1
            }, {
              label: '预约订单',
              value: 2
            }],
          },
          {
            label: "状态",
            prop: "status",
            search: true,
            type: 'select',
            dicData: [
              {
                label: '待付款',
                value: 0
              },
              {
                label: '待发货',
                value: 1
              },
              {
                label: '待收货',
                value: 2
              },
              {
                label: '待评价',
                value: 3
              },
              {
                label: '已完成',
                value: 4
              },
              {
                label: '已取消',
                value: 5
              }
            ],
          },
          {
            label: "下单时间开始",
            prop: "created_at_start",
            type: 'date',
            dateDefault: true,
            searchValue: '',      // 这是我获取的默认的显示日期，我这里是['2020-08-04', '2020-09-22']
            format: 'yyyy-MM-dd',                  // 这是组件展示的日期格式
            valueFormat: 'yyyy-MM-dd',        // 这是组件value值的格式
            search: true,
          },
          {
            label: "下单时间结束",
            prop: "created_at_end",
            type: 'date',
            dateDefault: true,
            searchValue: '',      // 这是我获取的默认的显示日期，我这里是['2020-08-04', '2020-09-22']
            format: 'yyyy-MM-dd',                  // 这是组件展示的日期格式
            valueFormat: 'yyyy-MM-dd',        // 这是组件value值的格式
            search: true,
          },
        ],
      },
      data: [],
    };
  },
  computed: {
    ...mapGetters(["permission"]),

    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.user_add, true),
        viewBtn: this.vaildData(this.permission.user_view, true),
        delBtn: this.vaildData(this.permission.user_delete, true),
        editBtn: this.vaildData(this.permission.user_edit, true),
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
  },
  mounted() {
    // 获取用户
    this.getUserData()
  },
  methods: {
    handleDelivery(row, index) {
      this.isShowModal = true
      this.currentData = row
    },
    resetForm() {
      this.isShowModal = false
      this.formData = {}
    },
    submitForm() {
      getDeliveryProductOrderData({
        "id": this.currentData.id,
        "logistics_name": this.formData.logistics_name,
        "logistics_tracking_no": this.formData.logistics_tracking_no,
      }).then((res) => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!",
        });
        this.resetForm()
      });
    },

    getUserData() {
      getSystemUserList({
        "page": 1,
        "size": 999999
      }).then((res) => {
        const resData = res.data.list;
        this.option.column[1].dicData = resData.map((r, index) => {
          return {
            label: r.nickname,
            value: r.id
          }
        })
      });
    },
    // 新增二级分类
    rowAdd(row, index) {
      return
      this.form.type = row.type;
      this.form.parent_id = row.id;
      this.option.column[0].addDisabled = true
      this.option.column[2].addDisabled = true
      this.$refs.crud.rowAdd(row, index);
    },
    rowEdit(row, index) {
      return
      this.$refs.crud.rowEdit(row, index);
    },
    rowDel(row, index) {
      this.$refs.crud.rowDel(row, index);
    },
    rowSave(row, done, loading) { },
    rowUpdate(row, index, done, loading) { },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", '提示', {
        cancelButtonText: "取消",
        confirmButtonText: "确定",
        type: "warning",
      })
        .then(() => {
          return xxx({ id: row.id });
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },

    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      done();
    },
    searchReset() {
      this.search = {
        type: 1
      }
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, search = {}) {
      this.loading = true;
      let param = {
        page: page.currentPage,
        size: page.pageSize,
      }
      getProductOrderList({ ...param, ...search }).then((res) => {
        const resData = res.data;
        this.data = resData.list;
        this.page.total = resData.total;
        this.loading = false;
        this.selectionClear();
      });
    },
    treeLoad(tree, treeNode, resolve) {
      // var id = tree.id;
      // get(Object.assign({  parentId: tree.id })).then((response) => {
      //   resolve(response.data.data);
      // })
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
  },
};
</script>

<style>
.el-switch__label * {
  line-height: 1;
  font-size: 12px;
  display: inline-block;
}

.el-switch__label {
  position: absolute;
  display: none;
  color: #fff !important;
  font-size: 12px !important;
}

/*打开时文字位置设置*/
.el-switch__label--right {
  z-index: 1;
  right: -6px;
}

/*关闭时文字位置设置*/
.el-switch__label--left {
  z-index: 1;
  left: 22px;
}

/*显示文字*/
.el-switch__label.is-active {
  display: block;
}

/*开关宽度*/
.el-switch .el-switch__core,
.el-switch .el-switch__label {
  width: 50px !important;
}
</style>
<style lang="scss" scoped>
// ::v-deep .el-table thead th .cell {
//   border-left: none;
// }

.normal {
  color: #409EFF;
  display: inline-block;
  display: flex;
  align-items: center;

  &::before {
    content: '';
    display: inline-block;
    width: 10px;
    height: 10px;
    border-radius: 10px;
    /**控制形状**/
    background-color: #409EFF;
    /**控制图标颜色**/
    margin-right: 4px
      /**图标与文字距离**/
  }
}

.un-normal {
  color: #d2d6de;
  display: inline-block;

  &::before {
    content: '';
    display: inline-block;
    width: 10px;
    height: 10px;
    border-radius: 10px;
    /**控制形状**/
    background-color: #d2d6de;
    /**控制图标颜色**/
    margin-right: 4px
      /**图标与文字距离**/
  }
}
</style>
